package capstoneproject.dudiachi.Main;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import capstoneproject.dudiachi.Controller.FestivalManager;
import capstoneproject.dudiachi.Controller.IslandArticleManager;
import capstoneproject.dudiachi.Controller.OldDistrictManager;
import capstoneproject.dudiachi.Controller.PlaceManager;
import capstoneproject.dudiachi.Model.Festival;
import capstoneproject.dudiachi.Model.IslandArticle;
import capstoneproject.dudiachi.Model.OldDistrict;
import capstoneproject.dudiachi.Model.Place;

import com.viewpagerindicator.CirclePageIndicator;

public class HomeActivity extends BaseActivity {
	private MyAdapter mAdapter;
	private ViewPager mPager;
	private CirclePageIndicator mIndicator;
	
	private IslandArticleManager biendao;
	private IslandArticle island;
	private PlaceManager diahinh;
	private Place place;
	private FestivalManager lehoi;
	private Festival festival;
	private OldDistrictManager diachi;
	private OldDistrict district;
	
	private Random random;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 5;
		setContentView(R.layout.activity_home);
		random = new Random();
		diachi = new OldDistrictManager(getApplicationContext());
		district = diachi.getListOldDistrict().get(random.nextInt(diachi.getListOldDistrict().size()));
		lehoi = new FestivalManager(getApplicationContext());
		festival = lehoi.getListFestival().get(random.nextInt(lehoi.getListFestival().size()));
		diahinh = new PlaceManager(getApplicationContext());
		place = diahinh.getListPlace().get(random.nextInt(diahinh.getListPlace().size()));
		biendao = new IslandArticleManager(getApplicationContext());
		island = biendao.getListIslandArticle().get(random.nextInt(biendao.getListIslandArticle().size()));
		
		mAdapter = new MyAdapter(getSupportFragmentManager(), district, festival, place, island);
		mPager = (ViewPager)findViewById(R.id.viewPager);
		mPager.setAdapter(mAdapter);
		
		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		mIndicator.setBackgroundColor(getResources().getColor(R.color.transparent));

	}

	private Bitmap getBitmapFromAsset(String strName) throws IOException {
		AssetManager assetManager = getAssets();
		BitmapFactory.Options options = new BitmapFactory.Options();
		InputStream istr = assetManager.open("img/" + strName);
		Bitmap bitmap = null;
		for(options.inSampleSize = 1; options.inSampleSize <=32 ; options.inSampleSize++){
			try{
				bitmap = BitmapFactory.decodeStream(istr,null, options);
				Log.d("SUCCESSFULLY", "Decode successfully at sample size: " + options.inSampleSize);
				break;
			}catch(OutOfMemoryError outOfMemoryError){
				Log.e("ERROR_OUT_MEMORY", "out of memory while reading sample size " + options.inSampleSize + " retrying with higher value");
			}
		}
		return bitmap;
	}
	
	public class MyAdapter extends FragmentPagerAdapter{

		private Festival festival;
		private Place place;
		private IslandArticle island;
		private OldDistrict district;
		private ArrayList<Bitmap> bitArray;
		private String title;
		
		public MyAdapter(android.support.v4.app.FragmentManager fm, OldDistrict district, Festival festival, Place place, IslandArticle island) {
			super(fm);
			this.district = district;
			this.festival = festival;
			this.place = place;
			this.island = island;
		}

		@Override
		public Fragment getItem(int i) {
			switch (i){
			case 0:
				title = getResources().getString(R.string.title_diachicandai);
				bitArray = new ArrayList<Bitmap>();
				try {
					bitArray.add(getBitmapFromAsset("Images/DiaChi/" + district.getImage1()));
				} catch (IOException e) {
					e.printStackTrace();
				}
				return new DuDiaChiFragment(getApplicationContext(), title, bitArray, district.getOldDistrictID(), district.getOldDistrictName(), randomDes(district.getDescription()));
			case 1:
				
				title = getResources().getString(R.string.title_diachilehoi);
				bitArray = new ArrayList<Bitmap>();
				try {
					bitArray.add(getBitmapFromAsset("Images/LeHoi/" + festival.getImage1()));
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				return new DuDiaChiFragment(getApplicationContext(), title, bitArray, festival.getFestivalID(), festival.getFestivalName(), randomDes(festival.getDescription()));
				
				case 2: 
				title = getResources().getString(R.string.title_diadanhthamquan);
				bitArray = new ArrayList<Bitmap>();
				try {
					bitArray.add(getBitmapFromAsset("Images/DiaDanh/" + place.getImage1()));
				} catch (IOException e) {
					e.printStackTrace();
				}
				return new DuDiaChiFragment(getApplicationContext(), title, bitArray, place.getPlaceID(), place.getPlaceName(), randomDes(place.getDescription()));
			case 3: 
				title = getResources().getString(R.string.title_biendaothanthuong);
				bitArray = new ArrayList<Bitmap>();
				try {
					bitArray.add(getBitmapFromAsset("Images/BienDao/" + island.getImage1()));
				} catch (IOException e) {
					e.printStackTrace();
				}
				return new DuDiaChiFragment(getApplicationContext(), title, bitArray, island.getIslandArticleID(), island.getIslandArticleName(), randomDes(island.getDescription()));
			default:
				return null;
			}
			
		}
	
		@Override
		public int getCount() {
			
			return 4;
		}
		
		public String randomDes(String str){
			ArrayList<String> strList = new ArrayList<String>();
			StringTokenizer token = new StringTokenizer(str,"|");
			while(token.hasMoreTokens()){
				strList.add(token.nextToken());
			}
			Random random = new Random();
			int choice = random.nextInt(strList.size());
			String pickedString = "";
			pickedString = strList.get(choice);
			
			return pickedString;
		}
		
	}

	
}
